<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
function func_navigation_get($link_array, $total, $limit_start, $limit_length, $max_page_links = '5', $page_name = 'page') {
	$page = $link_array['page'];
	$parameters = $link_array['parameters'];
	
	if( $limit_start <= 0 ) {
		$limit_start = 0;
	}
	if( $limit_length < 0 ) {
		$limit_length = 10;
	}
	//page
	$current_page_number = intval($limit_start / $limit_length);
	$current_page_number++;
	if( $limit_start % $limit_length ) {
		$current_page_number++;
	}
	$number_of_pages = intval($total / $limit_length);
	if( $total % $limit_length ) {
		$number_of_pages++;
	}
	//window
	$cur_window_num = intval($current_page_number / $max_page_links);
	if( $current_page_number % $max_page_links ) {
		$cur_window_num++;
	}
	$max_window_num = intval($number_of_pages / $max_page_links);
	if( $number_of_pages % $max_page_links ) {
		$max_window_num++;
	}
	
	//LINKS
	$links = '';
	// previous button - not displayed on first page
	if( $current_page_number > 1 ) {
		$links .= '<a href="' . util_href_link($page, $parameters . $page_name . '=' . ($current_page_number - 1)) . '" title="' . $lang_nav['pre_title'] . '">' . $lang_nav['pre'] . '</a>&nbsp;&nbsp;';
	}
	// previous window of pages
	if( $cur_window_num > 1 ) {
		$links .= '<a href="' . util_href_link($page, $parameters . $page_name . '=' . (($cur_window_num - 1) * $max_page_links)) . '" title="' . sprintf($lang_nav['pre_set_of_no_page_title'], $max_page_links) . '">' . $lang_nav['pre_set_of_no_page'] . '</a>&nbsp;&nbsp;';
	}
	// page nn button
	for($jump_to_page = 1 + (($cur_window_num - 1) * $max_page_links);
		 ($jump_to_page <= ($cur_window_num * $max_page_links)) && ($jump_to_page <= $number_of_pages); 
		 $jump_to_page++ ) {
		if( $jump_to_page == $current_page_number ) {
			$links .= '<b>' . $jump_to_page . '</b>&nbsp;&nbsp;';
		} else {
			$links .= '<a href="' . util_href_link($page, $parameters . $page_name . '=' . $jump_to_page) . '" title="' . sprintf($lang_nav['next_set_of_no_page'], $jump_to_page) . '">' . $jump_to_page . '</a>&nbsp;&nbsp;';
			
		}
	}
	// next window of pages
	if( $cur_window_num < $max_window_num ) {
		$links .= '<a href="' . util_href_link($page, $parameters . $page_name . '=' . (($cur_window_num) * $max_page_links + 1)) . '" title="' . sprintf($lang_nav['next_set_of_no_page_title'], $max_page_links) . '">' . $lang_nav['next_set_of_no_page'] . '</a>&nbsp;&nbsp;';
	}
	// next button
	if( ($current_page_number < $number_of_pages) && ($number_of_pages != 1) ) {
		$links .= '<a href="' . util_href_link($page, $parameters . $page_name . '=' . ($current_page_number + 1)) . '" title="' . $lang_nav['next_title'] . '">' . $lang_nav['next'] . '</a>&nbsp;&nbsp;';
	}
	
	//COUNT
	$from_num = $limit_start + 1;
	$to_num = $limit_start + $limit_length;
	if( $to_num >= $total ) {
		$to_num = $total;
	}
	$count = sprintf(NAVIGATION_COUNT_TEXT, $from_num, $to_num, $total);
	
	//Return
	$navigation = array(
				'links' => $links,
				'count' => $count,
					);
	return $navigation;
}

function func_navigation_common($link_array, $total, $limit_start, $limit_length, $max_page_links = '5', $page_name = 'page') {
	global $LANGUAGE;
	
	$page = $link_array['page'];
	$parameters = $link_array['parameters'];
	
	if( $limit_start <= 0 ) {
		$limit_start = 0;
	}
	if( $limit_length < 0 ) {
		$limit_length = 10;
	}
	//page
	$current_page_number = intval($limit_start / $limit_length);
	$current_page_number++;
	if( $limit_start % $limit_length ) {
		$current_page_number++;
	}
	$number_of_pages = intval($total / $limit_length);
	if( $total % $limit_length ) {
		$number_of_pages++;
	}
	//window
	$cur_window_num = intval($current_page_number / $max_page_links);
	if( $current_page_number % $max_page_links ) {
		$cur_window_num++;
	}
	$max_window_num = intval($number_of_pages / $max_page_links);
	if( $number_of_pages % $max_page_links ) {
		$max_window_num++;
	}
	
	//LINKS
	$links = '';
	// previous button - not displayed on first page
	if( $current_page_number > 1 ) {
		$links .= '<li><a href="' . util_href_link($page, $parameters . $page_name . '=' . ($current_page_number - 1)) . '" title="' . $LANGUAGE['navigation']['pre_title'] . '">' . $LANGUAGE['navigation']['pre'] . '</a></li>';
	}
	// previous window of pages
	if( $cur_window_num > 1 ) {
		$links .= '<li><a href="' . util_href_link($page, $parameters . $page_name . '=' . (($cur_window_num - 1) * $max_page_links)) . '" title="' . sprintf($LANGUAGE['navigation']['pre_set_of_no_page_title'], $max_page_links) . '">' . $LANGUAGE['navigation']['pre_set_of_no_page'] . '</a></li>';
	}
	// page nn button
	for($jump_to_page = 1 + (($cur_window_num - 1) * $max_page_links);
		 ($jump_to_page <= ($cur_window_num * $max_page_links)) && ($jump_to_page <= $number_of_pages); 
		 $jump_to_page++ ) {
		if( $jump_to_page == $current_page_number ) {
			$links .= '<li><b>' . $jump_to_page . '</b></li>';
		} else {
			$links .= '<li><a href="' . util_href_link($page, $parameters . $page_name . '=' . $jump_to_page) . '" title="' . sprintf($LANGUAGE['navigation']['page_no_title'], $jump_to_page) . '">' . $jump_to_page . '</a></li>';
			
		}
	}
	// next window of pages
	if( $cur_window_num < $max_window_num ) {
		$links .= '<li><a href="' . util_href_link($page, $parameters . $page_name . '=' . (($cur_window_num) * $max_page_links + 1)) . '" title="' . sprintf($LANGUAGE['navigation']['next_set_of_no_page_title'], $max_page_links) . '">' . $LANGUAGE['navigation']['next_set_of_no_page'] . '</a></li>';
	}
	// next button
	if( ($current_page_number < $number_of_pages) && ($number_of_pages != 1) ) {
		$links .= '<li><a href="' . util_href_link($page, $parameters . $page_name . '=' . ($current_page_number + 1)) . '" title="' . $LANGUAGE['navigation']['next_title'] . '">' . $LANGUAGE['navigation']['next'] . '</a></li>';
	}
	
	//COUNT
	$from_num = $limit_start + 1;
	$to_num = $limit_start + $limit_length;
	if( $to_num >= $total ) {
		$to_num = $total;
	}
	$count = sprintf($LANGUAGE['navigation']['count'], $from_num, $to_num, $total);
	
	//SHOW
	if( $total <= $limit_length ) {
		$show = '0';
	} else {
		$show = '1';
	}
	
	//Return
	$navigation = array(
				'show' => $show,
				'links' => $links,
				'count' => $count,
					);
	return $navigation;
}

function util_common_navigation_ds($link_array, $total, $limit_start, $limit_length, $max_page_links = '5', $page_name = 'page') {
	global $LANGUAGE;
	$lang_nav = $LANGUAGE['navigation'];
	
	//
	$page = $link_array['page'];
	$parameters = $link_array['parameters'];
	
	if( $limit_start <= 0 ) {
		$limit_start = 0;
	}
	if( $limit_length < 0 ) {
		$limit_length = 10;
	}
	//page
	$current_page_number = intval($limit_start / $limit_length);
	$current_page_number++;
	if( $limit_start % $limit_length ) {
		$current_page_number++;
	}
	$number_of_pages = intval($total / $limit_length);
	if( $total % $limit_length ) {
		$number_of_pages++;
	}
	//window
	$cur_window_num = intval($current_page_number / $max_page_links);
	if( $current_page_number % $max_page_links ) {
		$cur_window_num++;
	}
	$max_window_num = intval($number_of_pages / $max_page_links);
	if( $number_of_pages % $max_page_links ) {
		$max_window_num++;
	}
	
	//LINKS
	$links = '';
	// previous button - not displayed on first page
	if( $current_page_number > 1 ) {
		$links .= '<li class="prev"><a href="' . util_href_link($page, $parameters . $page_name . '=' . ($current_page_number - 1)) . '" title="' . $lang_nav['pre_title'] . '">' . $lang_nav['pre'] . '</a></li>';
	}
	// first page
	if( $cur_window_num > 1 ) {
		$links .= '<li><a href="' . util_href_link($page, $parameters . $page_name . '=1') . '" title="' . $lang_nav['first_title'] . '">1</a></li>';
	}
	// previous window of pages
	if( $cur_window_num > 1 ) {
		$links .= '<li class="prevp"><a href="' . util_href_link($page, $parameters . $page_name . '=' . (($cur_window_num - 1) * $max_page_links)) . '" title="' . sprintf($lang_nav['pre_set_of_no_page_title'], $max_page_links) . '">' . $lang_nav['pre_set_of_no_page'] . '</a></li>';
	}
	// page nn button
	for($jump_to_page = 1 + (($cur_window_num - 1) * $max_page_links);
		 ($jump_to_page <= ($cur_window_num * $max_page_links)) && ($jump_to_page <= $number_of_pages); 
		 $jump_to_page++ ) {
		if( $jump_to_page == $current_page_number ) {
			$links .= '<li><b>[' . $jump_to_page . ']</b></li>';
		} else {
			$links .= '<li><a href="' . util_href_link($page, $parameters . $page_name . '=' . $jump_to_page) . '" title="' . sprintf($lang_nav['page_no_title'], $jump_to_page) . '">' . $jump_to_page . '</a></li>';
			
		}
	}
	// next window of pages
	if( $cur_window_num < $max_window_num ) {
		$links .= '<li class="nextp"><a href="' . util_href_link($page, $parameters . $page_name . '=' . (($cur_window_num) * $max_page_links + 1)) . '" title="' . sprintf($lang_nav['next_set_of_no_page_title'], $max_page_links) . '">' . $lang_nav['next_set_of_no_page'] . '</a></li>';
	}
	// last page
	if( ($current_page_number < $number_of_pages) && ($number_of_pages != 1) && $cur_window_num < $max_window_num ) {
		$links .= '<li><a href="' . util_href_link($page, $parameters . $page_name . '=' . ($number_of_pages)) . '" title="' . $lang_nav['last_title'] . '">' . $number_of_pages . '</a></li>';
	}
	// next button
	if( ($current_page_number < $number_of_pages) && ($number_of_pages != 1) ) {
		$links .= '<li class="next"><a href="' . util_href_link($page, $parameters . $page_name . '=' . ($current_page_number + 1)) . '" title="' . $lang_nav['next_title'] . '">' . $lang_nav['next'] . '</a></li>';
	}
	
	//COUNT
	$from_num = $limit_start + 1;
	$to_num = $limit_start + $limit_length;
	if( $to_num >= $total ) {
		$to_num = $total;
	}
	$count = sprintf($lang_nav['count'], $from_num, $to_num, $total);
	
	//SHOW
	if( $total <= $limit_length ) {
		$show = '0';
	} else {
		$show = '1';
	}
	
	//RETURN
	$navigation = array(
				'show' => $show,
				'links' => $links,
				'count' => $count,
					);
	return $navigation;
}

function func_navigation_common_js($js, $link_array, $total, $limit_start, $limit_length, $max_page_links = '5', $page_name = 'page') {
	global $LANGUAGE;
	
	$page = $link_array['page'];
	$parameters = $link_array['parameters'];
	
	if( $limit_start <= 0 ) {
		$limit_start = 0;
	}
	if( $limit_length < 0 ) {
		$limit_length = 10;
	}
	//page
	$current_page_number = intval($limit_start / $limit_length);
	$current_page_number++;
	if( $limit_start % $limit_length ) {
		$current_page_number++;
	}
	$number_of_pages = intval($total / $limit_length);
	if( $total % $limit_length ) {
		$number_of_pages++;
	}
	//window
	$cur_window_num = intval($current_page_number / $max_page_links);
	if( $current_page_number % $max_page_links ) {
		$cur_window_num++;
	}
	$max_window_num = intval($number_of_pages / $max_page_links);
	if( $number_of_pages % $max_page_links ) {
		$max_window_num++;
	}
	
	//LINKS
	$links = '';
	// previous button - not displayed on first page
	if( $current_page_number > 1 ) {
		$links .= '<li><a href="#' . util_href_link($page, $parameters . $page_name . '=' . ($current_page_number - 1)) . '" onclick="' . $js . '(' . ($current_page_number - 1) . ');return false;" title="' . $LANGUAGE['navigation']['pre_title'] . '">' . $LANGUAGE['navigation']['pre'] . '</a></li>';
	}
	// previous window of pages
	if( $cur_window_num > 1 ) {
		$links .= '<li><a href="#' . util_href_link($page, $parameters . $page_name . '=' . (($cur_window_num - 1) * $max_page_links)) . '" onclick="' . $js . '(' . (($cur_window_num - 1) * $max_page_links) . ');return false;" title="' . sprintf($LANGUAGE['navigation']['pre_set_of_no_page_title'], $max_page_links) . '">' . $LANGUAGE['navigation']['pre_set_of_no_page'] . '</a></li>';
	}
	// page nn button
	for($jump_to_page = 1 + (($cur_window_num - 1) * $max_page_links);
		 ($jump_to_page <= ($cur_window_num * $max_page_links)) && ($jump_to_page <= $number_of_pages); 
		 $jump_to_page++ ) {
		if( $jump_to_page == $current_page_number ) {
			$links .= '<li><b>' . $jump_to_page . '</b></li>';
		} else {
			$links .= '<li><a href="#' . util_href_link($page, $parameters . $page_name . '=' . $jump_to_page) . '" onclick="' . $js . '(' . $jump_to_page . ');return false;" title="' . sprintf($LANGUAGE['navigation']['page_no_title'], $jump_to_page) . '">' . $jump_to_page . '</a></li>';
			
		}
	}
	// next window of pages
	if( $cur_window_num < $max_window_num ) {
		$links .= '<li><a href="#' . util_href_link($page, $parameters . $page_name . '=' . (($cur_window_num) * $max_page_links + 1)) . '" onclick="' . $js . '(' . (($cur_window_num) * $max_page_links + 1) . ');return false;" title="' . sprintf($LANGUAGE['navigation']['next_set_of_no_page_title'], $max_page_links) . '">' . $LANGUAGE['navigation']['next_set_of_no_page'] . '</a></li>';
	}
	// next button
	if( ($current_page_number < $number_of_pages) && ($number_of_pages != 1) ) {
		$links .= '<li><a href="#' . util_href_link($page, $parameters . $page_name . '=' . ($current_page_number + 1)) . '" onclick="' . $js . '(' . ($current_page_number + 1) . ');return false;" title="' . $LANGUAGE['navigation']['next_title'] . '">' . $LANGUAGE['navigation']['next'] . '</a></li>';
	}
	
	//COUNT
	$from_num = $limit_start + 1;
	$to_num = $limit_start + $limit_length;
	if( $to_num >= $total ) {
		$to_num = $total;
	}
	$count = sprintf($LANGUAGE['navigation']['count'], $from_num, $to_num, $total);
	
	//SHOW
	if( $total <= $limit_length ) {
		$show = '0';
	} else {
		$show = '1';
	}
	
	//Return
	$navigation = array(
				'show' => $show,
				'links' => $links,
				'count' => $count,
					);
	return $navigation;
}

function func_navigation_common_jsc($js, $link_array, $total, $limit_start, $limit_length, $limit_count = 0, $max_page_links = '5', $page_name = 'page') {
	global $LANGUAGE;
	
	if( $limit_count == 0 ) {
		$limit_count = $limit_length;
	}
	
	$page = $link_array['page'];
	$parameters = $link_array['parameters'];
	
	if( $limit_start <= 0 ) {
		$limit_start = 0;
	}
	if( $limit_length < 0 ) {
		$limit_length = 10;
	}
	//page
	$current_page_number = intval($limit_start / $limit_length);
	$current_page_number++;
	if( $limit_start % $limit_length ) {
		$current_page_number++;
	}
	$number_of_pages = intval($total / $limit_length);
	if( $total % $limit_length ) {
		$number_of_pages++;
	}
	//window
	$cur_window_num = intval($current_page_number / $max_page_links);
	if( $current_page_number % $max_page_links ) {
		$cur_window_num++;
	}
	$max_window_num = intval($number_of_pages / $max_page_links);
	if( $number_of_pages % $max_page_links ) {
		$max_window_num++;
	}
	
	//LINKS
	$links = '';
	// previous button - not displayed on first page
	if( $current_page_number > 1 ) {
		$links .= '<li><a href="#' . util_href_link($page, $parameters . $page_name . '=' . ($current_page_number - 1)) . '" onclick="' . $js . '(' . ($current_page_number - 1) . ');return false;" title="' . $LANGUAGE['navigation']['pre_title'] . '">' . $LANGUAGE['navigation']['pre'] . '</a></li>';
	}
	// previous window of pages
	if( $cur_window_num > 1 ) {
		$links .= '<li><a href="#' . util_href_link($page, $parameters . $page_name . '=' . (($cur_window_num - 1) * $max_page_links)) . '" onclick="' . $js . '(' . (($cur_window_num - 1) * $max_page_links) . ');return false;" title="' . sprintf($LANGUAGE['navigation']['pre_set_of_no_page_title'], $max_page_links) . '">' . $LANGUAGE['navigation']['pre_set_of_no_page'] . '</a></li>';
	}
	// page nn button
	for($jump_to_page = 1 + (($cur_window_num - 1) * $max_page_links);
		 ($jump_to_page <= ($cur_window_num * $max_page_links)) && ($jump_to_page <= $number_of_pages); 
		 $jump_to_page++ ) {
		if( $jump_to_page == $current_page_number ) {
			$links .= '<li><b>' . $jump_to_page . '</b></li>';
		} else {
			$links .= '<li><a href="#' . util_href_link($page, $parameters . $page_name . '=' . $jump_to_page) . '" onclick="' . $js . '(' . $jump_to_page . ');return false;" title="' . sprintf($LANGUAGE['navigation']['page_no_title'], $jump_to_page) . '">' . $jump_to_page . '</a></li>';
			
		}
	}
	// next window of pages
	if( $cur_window_num < $max_window_num ) {
		$links .= '<li><a href="#' . util_href_link($page, $parameters . $page_name . '=' . (($cur_window_num) * $max_page_links + 1)) . '" onclick="' . $js . '(' . (($cur_window_num) * $max_page_links + 1) . ');return false;" title="' . sprintf($LANGUAGE['navigation']['next_set_of_no_page_title'], $max_page_links) . '">' . $LANGUAGE['navigation']['next_set_of_no_page'] . '</a></li>';
	}
	// next button
	if( ($current_page_number < $number_of_pages) && ($number_of_pages != 1) ) {
		$links .= '<li><a href="#' . util_href_link($page, $parameters . $page_name . '=' . ($current_page_number + 1)) . '" onclick="' . $js . '(' . ($current_page_number + 1) . ');return false;" title="' . $LANGUAGE['navigation']['next_title'] . '">' . $LANGUAGE['navigation']['next'] . '</a></li>';
	}
	
	//COUNT
	$from_num = $limit_start + 1;
	$to_num = $limit_start + $limit_count;
	if( $to_num >= $total ) {
		$to_num = $total;
	}
	$count = sprintf($LANGUAGE['navigation']['count'], $from_num, $to_num, $total);
	
	//SHOW
	if( $total <= $limit_length ) {
		$show = '0';
	} else {
		$show = '1';
	}
	
	//Return
	$navigation = array(
				'show' => $show,
				'links' => $links,
				'count' => $count,
					);
	return $navigation;
}
//
?>